import { Component, Vue } from 'vue-property-decorator'
import InfoBlock from '@profile/util/info_block/Index.vue';
import axios from '@axios/user';
import { UserHiddenRoots } from '@entity/user';

@Component({
    components: {
        InfoBlock
    }
})
export class HiddenInfo extends Vue {

    /** 源数据 */
    private source: String = '';

    /** 隐藏权限 */
    private infoHidden: UserHiddenRoots = new UserHiddenRoots();

    /**
     * 从服务器获取当前用户的隐藏权限,
     * 并存为源数据
     */
    created() {
        axios.roots.queryHidden(this.$store.getters.me().id).then((res: UserHiddenRoots) => {
            if (res == undefined) {
                res = new UserHiddenRoots();
            }
            this.infoHidden = res;
            this.source = JSON.stringify(res);
        });
    }

    /**
     * 更新信息, 服务端更新成功后才更新本地
     */
    private save(): void {
        axios.roots.updateHidden(this.infoHidden).then(res => {
            if (res) {
                this.source = JSON.stringify(this.infoHidden);
            }
        });
    }

    /**
     * 判断是否进行了修改
     * 只需要将当前数据与源数据进行比较即可
     */
    private changed(): Boolean {
        return this.source.isEmpty() || JSON.stringify(this.infoHidden) === this.source;
    }

}